class Scheduler {
	constructor(limit) {
		this.limit = limit
		this.queue = []
	}

	add(time, order) {
		const promiseCreator = () => {
			return new Promise((resolve, reject) => {
				setTimeout(() => {
					console.log(order);
					resolve()
				}, time)
			})
		}
		this.queue.push(promiseCreator)
	}

	taskStart() {
		for (let i = 0; i < this.limit; i++) {
			this.request()
		}
	}

	request() {
		if (!this.queue.length) return
		let task = this.queue.shift()
		task().then(() => {
			this.request()
		})
	}

}

let scheduler = new Scheduler(2)

const addTask = (time, order) => {
	scheduler.add(time, order)
}

addTask(1000, '1')
addTask(500, '2')
addTask(300, '3')
addTask(400, '4')

scheduler.taskStart()

// 2314
